#!/usr/local/bin/ruby
RAILS_ROOT = File.expand_path(File.dirname(__FILE__) + "/../..")

# hack to get local config
server_url = ""
File.open("#{RAILS_ROOT}/config/initializers/conf-local.rb") do |f|
  f.read.match(/server_root_path = "(.+)"/)
  server_url = $1
end

tempfile = "#{RAILS_ROOT}/tmp/emailtemp"

# redirect stdin into tempfile
File.open(tempfile, "w"){|f| f.write(ARGF.read)}

# call curl to do post request
`curl -F "file=@#{tempfile}" #{server_url}/sources/receive_email?iamarobot=1`

# delete tempfile
File.delete(tempfile)
